package com.controller;

import com.alibaba.druid.sql.visitor.functions.Right;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.RightClassOne;
import com.pojo.RightKey;
import com.service.RightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/rightController")
public class RightController {

    @Autowired
    private RightService rightService;


    @ResponseBody
    @RequestMapping("/deleteRight")
    public String deleteRight(@RequestBody RightClassOne rightClassOne){
        if (rightClassOne.getRightSuperId()!=0) {
            Boolean isok = rightService.updateStatusByRightId(rightClassOne.getRightId());
            if (isok) {
                return "true";
            }
            return "false";
        }else {
            Boolean isok = rightService.updateStatusFatherAndChild(rightClassOne.getRightId());
            if (isok) {
                return "true";
            }
            return "false";
        }
    }

    @ResponseBody
    @RequestMapping("/updateRight")
    public String updateRight(@RequestBody RightClassOne rightClassOne){
        System.out.println(rightClassOne);
        Boolean isok = rightService.updateRight(rightClassOne);
        if (isok) {
            return "true";
        }
        return "false";
    }

    @ResponseBody
    @RequestMapping("/showMainRight")
    public List<RightClassOne> showMainRight(){
        List<RightClassOne> list = rightService.findMainRight();
        return list;
    }

    @ResponseBody
    @RequestMapping("/addRight")
    public String addRight(@RequestBody RightKey rightKey){
        RightClassOne rightClassOne = rightKey.getRightClassOne();
        Integer key = rightKey.getAddType();
        rightClassOne.setCreateDay(new Date());
        if (key==0){
            Boolean isok = rightService.addFrontRight(rightClassOne);
            if (isok) {
                return "true";
            }
            return "false";
        }else if (key==1){
            Boolean isok = rightService.addBackgroundRight(rightClassOne);
            if (isok) {
                return "true";
            }
            return "false";
        }
        return "false";
    }

    /**
     * 首页展示权限列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/fuzzySelectRight")
    public PageInfo<RightClassOne> fuzzySelectRight(@RequestBody RightKey rightKey){
        RightClassOne rightClassOne = rightKey.getRightClassOne();
        Integer page = rightKey.getPage();
        if (page==null){
            page=1;
        }
        PageHelper.startPage(page,7);
        List<RightClassOne> list = rightService.findByFuzzy(rightClassOne);
        PageInfo<RightClassOne> pageInfo = new PageInfo<RightClassOne>(list, 5);
        return pageInfo;
    }
}
